檢查是否reverse也是相同的
EX: anna = anna
function Palindrome(str){
let left = 0
let right = str.length -1
while(left <= right){
if(str[left] === str[right]){
left++
right--
}else{
return false
}
}return true
}
console.log(Palindrome('hcco')) //false
檢查是否為子序列
function subsequence(str1, str2){ //str1為原始序列, str2為子序列
if(str1.length == 0 ){
return true
}
let pointer1 = 0 //設置兩個指標,逐一比對
let pointer2 = 0
while(str2.length > pointer2){
if(str1[pointer1] == str2[pointer2]){ //若str1 == str2 str1[pointer] ++ 繼續比對
pointer1++
}
if(pointer1 >= str1.length){
return true //如果pointer已經 == str1長度 代表都符合 即回傳true
}
pointer2++ //每次執行都要比對str2[pointer] 所以++
}return false ///若都不符合即回傳false
}
console.log(subsequence("hello", "hssello")) //true
console.log(subsequence("hello", "ehllo")) //false